#include "swpabstractdocument.h"

#include <icontainerdocument.h>

#include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>

using namespace core;
using namespace swp;

SwpAbstractDocument::SwpAbstractDocument(const QString& name,
    const MimeType& mimeType,
    const QSharedPointer<core::IContainerDocument>& container,
    bool isFuture) : IDocument(),
        m_valid(false),
        m_name(name),
        m_mimeType(mimeType),
        m_isDirty(false),
        m_isFuture(isFuture),
        m_container(container),
        m_data()
{
    const int width = QApplication::desktop()->width();
    const int height = QApplication::desktop()->height();

    m_data.setSceneRect(0, 0, width, height);

    connect(&m_data, SIGNAL(dataChanged()),
        this, SLOT(makeDirty()));
}

SwpAbstractDocument::~SwpAbstractDocument()
{

}

void SwpAbstractDocument::setValid(bool valid)
{
    if (m_valid != valid)
    {
        m_valid = valid;
        makeDirty();
        emit validityChanged(valid);
    }
}

bool SwpAbstractDocument::isValid() const
{
    return m_valid;
}

bool SwpAbstractDocument::isFuture() const
{
    return m_isFuture;
}

bool SwpAbstractDocument::isLinked() const
{
    return false;
}

bool SwpAbstractDocument::isContainer() const
{
    return false;
}

bool SwpAbstractDocument::isDirty() const
{
    return m_isDirty;
}

QString SwpAbstractDocument::name() const
{
    return m_name;
}

core::MimeType SwpAbstractDocument::mimeType() const
{
    return m_mimeType;
}

QSharedPointer<core::IContainerDocument>
    SwpAbstractDocument::container() const
{
    return m_container;
}

void SwpAbstractDocument::setDirty(bool dirty)
{    
    if (m_isDirty != dirty)
    {
        m_isDirty = dirty;

        emit dirtyChanged(m_isDirty);
    }    
}

void SwpAbstractDocument::setFuture(bool future)
{
    if (m_isFuture != future)
    {
        m_isFuture = future;
    }
}

void SwpAbstractDocument::setContainer(
    const QSharedPointer<IContainerDocument>& container)
{
    if (m_container != container)
    {
        m_container = container;
        emit containerChanged();
    }
}

void SwpAbstractDocument::rename(const QString& name)
{
    if (m_name != name)
    {
        m_name = name;
        emit nameChanged();
    }
}

IDocumentData* SwpAbstractDocument::data() const
{
    return &m_data;
}

void SwpAbstractDocument::makeDirty()
{
    setDirty();
}
